package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.SystemUser;
import com.czh.springboot04.service.SystemUserService;
import com.czh.springboot04.util.MD5Util;
import com.czh.springboot04.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
//@CrossOrigin
@RequestMapping("/us")
public class SystemUserController {
    @Autowired
    SystemUserService systemUserService;


    // 动态的获取数据
    @RequestMapping("/getTB")
    public R getTB(){
       /* [{value: 1048,name: 'Search Engine'},
        {value: 735,name: 'Direct'},{value: 580,name: 'Email'},
        {value: 484,name: 'Union Ads'},{value: 300,name: 'Video Ads'}]*/

        List<HashMap<String,Object>> list = systemUserService.selectCount();

        return R.ok(list);
    }
    @PostMapping("/register")
    public R add(@RequestBody SystemUser systemUser){
        //System.out.println(systemUser);
        String token = UUID.randomUUID().toString().replace("-","").substring(0,5);
        systemUser.setToken(token);
        systemUser.setPassword(MD5Util.encode(systemUser.getUsername()+systemUser.getPassword()));
        int add = systemUserService.add(systemUser);
        return R.ok(add);
    }

    @GetMapping("/passwordCheck")
    public R passwordCheck(String p1,String un,String p2){
        //System.out.println(p1);
        //System.out.println(un);
        //System.out.println(MD5Util.encode(un+p2));
        if(p1.equalsIgnoreCase(MD5Util.encode(un+p2))){
            return R.ok("密码正确");
        }
        return R.fail("密码错误");
    }

    @PutMapping("/editPassword")
    public R editPassword(@RequestBody SystemUser systemUser){
        System.out.println(systemUser);
        systemUser.setPassword(MD5Util.encode(systemUser.getUsername()+systemUser.getPassword()));
        int edit = systemUserService.editSu(systemUser);
        return R.ok(edit);
    }




    @PostMapping("/loginSu")
    public R loginSu(@RequestBody SystemUser systemUser){
        //System.out.println(systemUser);
        SystemUser su = systemUserService.findByUsername(systemUser);
        if(su!=null){
            if(su.getPassword().equalsIgnoreCase(MD5Util.encode(systemUser.getUsername()+systemUser.getPassword()))){
                return R.ok(su);
            }else{
                return R.fail("-2");//密码错误
            }
        }else{
            return R.fail("-1");//用户名错误
        }
    }

    @RequestMapping("/login")
    public R login(@RequestBody SystemUser systemUser){
        //System.out.println(systemUser);
        SystemUser su = systemUserService.login(systemUser);
        if(su==null)
            return R.fail("用户名或密码错误");
        return R.ok(su);
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable("id") Integer id){
        SystemUser systemUser = systemUserService.findById(id);
        return R.ok(systemUser);
    }

    @GetMapping("/findAll")
    public R findAll(){
        return R.ok(systemUserService.findAll());
    }

    @GetMapping("/findConsult")
    public R findConsult(){
        return R.ok(systemUserService.findByRole(2));
    }

    @PutMapping("/edit")
    public R edit(@RequestBody SystemUser systemUser){
        int edit = systemUserService.editSu(systemUser);
        return R.ok(edit);
    }


    @GetMapping("/findByUn")
    public R findByUn(SystemUser systemUser){
        SystemUser su = systemUserService.findByUsername(systemUser);
        if(su==null)
            return R.fail("用户名不存在");
        return R.ok(su);
    }


}
